home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 February / WN_129_CD.iso / Windows / Extensions Firefox / Bookmarks Synchronizer / bookmarksftp1_0_1.xpi / chrome / bookmarksftp.jar / content / bmsyncNetworkService.js next >
Text File  |  2004-10-17  |  4KB  |  138 lines

  1. //upload.xul, download.xul, and browser.xul(Overlay)
  2.  
  3. var gBookmarksSynchronizerUploadService=
  4. {
  5.   _channel:null,
  6.   _callback:null,
  7.   _data:"",
  8.   _scheme:"",
  9.   _errorData:"",
  10.  
  11.   start:function(aStr,aURI,aType,aCallback)
  12.   {
  13.     if( !aStr || !aURI)
  14.       return false;
  15.     this._callback=aCallback;
  16.     this._scheme=aURI.scheme;
  17.     const ioService  = Components.classes["@mozilla.org/network/io-service;1"]
  18.                       .getService(Components.interfaces.nsIIOService);
  19.     const stringStream=Components.classes["@mozilla.org/io/string-input-stream;1"]
  20.                       .createInstance(Components.interfaces.nsIStringInputStream);
  21.     this._channel = ioService.newChannelFromURI( aURI )
  22.                       .QueryInterface(Components.interfaces.nsIUploadChannel);
  23.     try{
  24.       stringStream.setData(aStr, -1);
  25.       this._channel.setUploadStream(stringStream,aType,-1);
  26.       this._channel.asyncOpen(this, null);
  27.       this._callback("send",status);
  28.       this._data=aStr;
  29.       return true;
  30.     }catch(e){window.alert("__netwerk__\n\n"+"e");}
  31.     return false;
  32.   },
  33.  
  34.   cancel:function()
  35.   {
  36.     if(this._channel)
  37.       this._channel.cancel(0x804b0002);
  38.   },
  39.  
  40.   onDataAvailable: function (channel, ctxt, input, sourceOffset, count){
  41.     const sis=Components.classes["@mozilla.org/scriptableinputstream;1"]
  42.                       .createInstance(Components.interfaces.nsIScriptableInputStream);
  43.     sis.init(input);
  44.     this._errorData +=sis.read(count);
  45.   },
  46.   onStartRequest: function (channel, ctxt){},
  47.   onStopRequest: function (channel, ctxt, status)
  48.   {
  49.     if(this._scheme != "ftp"){
  50.       var res=0;
  51.       try{
  52.         res = channel.QueryInterface(Components.interfaces.nsIHttpChannel)
  53.                        .responseStatus;
  54.       }catch(e){}
  55.       if(res==200||res==201 || res==204)
  56.         status=0;
  57.       /*
  58.         200:OK
  59.         201:Created
  60.         204:No Content
  61.         This is an uploading channel, no need to "GET" the file contents.
  62.       */
  63.       if(this._errorData)
  64.         status=res;
  65.       if(this._errorData && res==200)
  66.         alert(this._errorData);
  67.     }
  68.     if(this._callback)
  69.       this._callback("done",status);
  70.   }
  71. };
  72.  
  73. var gBookmarksSynchronizerDownloadService=
  74. {
  75.   _channel:null,
  76.   streamLoader:null,
  77.   data:null,
  78.   length:null,
  79.  
  80.   _callback:null,
  81.   _startTime:0,
  82.   _endTime:0,
  83.  
  84.   start:function(aURI,aCallback)
  85.   {
  86.     if( !aURI )
  87.       return false;
  88.  
  89.     this._callback=aCallback;
  90.  
  91.     try{
  92.       var ioService  = Components.classes["@mozilla.org/network/io-service;1"]
  93.                       .getService(Components.interfaces.nsIIOService);
  94.       this.streamLoader=Components.classes["@mozilla.org/network/stream-loader;1"]
  95.                     .createInstance(Components.interfaces.nsIStreamLoader);
  96.       this._channel = ioService.newChannelFromURI( aURI );
  97.       if(aURI.scheme=="http" || aURI.scheme=="https")
  98.         this._channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
  99.       this.streamLoader.init(this._channel, this , null);
  100.       this._startTime=(new Date()).getTime();
  101.     }catch(e){ alert(e); return false;}
  102.     return true;
  103.   },
  104.  
  105.   cancel:function()
  106.   {
  107.     if(this._channel)
  108.       this._channel.cancel(0x804b0002);
  109.   },
  110.  
  111.   onStreamComplete :function ( loader , ctxt , status , resultLength , result )
  112.   {
  113.     this.data="";
  114.     this._endTime=(new Date()).getTime();
  115.     if(status==0)
  116.     {
  117.       this.length=resultLength;
  118.       if(typeof(result)=="string")
  119.         this.data=result;
  120.       else
  121.       {
  122.         while(result.length > (256*192) )
  123.         {
  124.           this.data += String.fromCharCode.apply(this,result.splice(0,256*192));
  125.         }
  126.         this.data += String.fromCharCode.apply(this,result);
  127.       }
  128.     }
  129.  
  130.     if(this._callback)
  131.       this._callback("done",status);
  132.   },
  133.  
  134.   get time(){
  135.     return this._endTime-this._startTime;
  136.   }
  137. };
  138.